<?php

namespace App\Http\Middleware;

use App\Http\Model\LoginTokenModel;
use Closure;

class HeaderToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(array_key_exists("token",$request->header()))
        {
            $isLogin = LoginTokenModel::where("token",$request->header()["token"][0])->first();

            if(empty($isLogin))
            {

                return response()->json($this->errToken());

            }

            return $next($request);
        }else
        {
            return response()->json($this->errToken());
        }
    }

    private function errToken()
    {
        $res = array();

        $res["code"] = ERROR_CODE;

        $res["msg"] = "TOKEN异常";

        return $res;
    }
}
